package Exam.VerificationCode;

import java.util.Random;

public class verificationcode {

    public static void main(String[] args) {
        
            /*
      需求:
        定义方法实现随机产生一个5位的验证码
        验证码格式:
        1.长度为5
        2.前四位是大写字母或者小写字母
        3.最后一位是数字   */


        //A-Z a-z , 0-9
        //放入数组

        //(1)创建一个容纳a-Z的数组
        //26个英文字母 * 2 = 52个
        //A-Z(41-90)  a-z(97-122)
        char[] chs = new char[52];
        //利用ASCII码表 a == 97
        for (int i = 0; i < (chs.length / 2); i++) {
            //将数字强转为字符
            chs[i] = (char) (97 + i);
            chs[i + 26] = (char) (65 + i);
        }

        //(2)随机索引，抽取索引
        Random r = new Random();
        char[] indexarr = new char[4];
        //随机获取4次索引
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(chs.length);
            indexarr[i] = chs[index];
            System.out.print(indexarr[i] + " ");
        }
        //(3)随机数字 0-9
        int num = r.nextInt(10);
        System.out.println(num);

    }

}
